<template>
  <div class="analysis">
    <div class="analysis-title">
      {{ id == 1 ? "全投资财务内部收益率(所得税前)" : "资本金财务内部收益率" }}
    </div>
    <el-table :data="analysisData" border style="width: 100%" v-loading="loading" :span-method="objectSpanMethod">
      <el-table-column width="200px" align="center"> 主方案 </el-table-column>
      <el-table-column prop="参数" label="指标" />
      <el-table-column prop="变化量" label="变化量" />
      <el-table-column prop="变化幅度" label="变化幅度" />
      <el-table-column prop="临界值" label="临界值" />
      <el-table-column v-if="id == 1" prop="所得内部收益率" label="所得内部收益率" />
      <el-table-column v-if="id == 2" prop="资本金基准收益率" label="所得内部收益率" />
    </el-table>
  </div>
</template>

<script setup>
import { onMounted, onUpdated } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { apiAnalysis_1, apiAnalysis_2 } from "../apis/apis.js";
import { raw_data } from "../tableData/data.js";
import allParams from "../utils/sendParams.js";

const router = useRouter();
const route = useRoute();

const id = ref(parseInt(route.params.aid));
const loading = ref(false);
const raw_data_ref = ref(raw_data);
const sendData = ref(allParams(raw_data_ref.value));

const analysisData = ref([]);

const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    if (rowIndex === 0) {
      return {
        rowspan: 3,
        colspan: 1,
      };
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};

const getATable = (id) => {
  loading.value = true
  if (id == 1) {
    apiAnalysis_1(sendData.value)
      .then((res) => {
        console.log("OK ", res);
        if (res.code === 200) {
          analysisData.value = res.obj;
          loading.value = false;
        }
      })
      .catch((res) => {
        console.log("ERROR ", res);
        ElMessage({
          message: "参数计算错误",
          grouping: true,
          type: "error",
        });
      });
  }
  if (id == 2) {
    apiAnalysis_2(sendData.value)
      .then((res) => {
        console.log("OK ", res);
        if (res.code === 200) {
          analysisData.value = res.obj;
          loading.value = false;
        }
      })
      .catch((res) => {
        console.log("ERROR ", res);
        ElMessage({
          message: "参数计算错误",
          grouping: true,
          type: "error",
        });
      });
  }
};

onMounted(() => {
  console.log(" onMounted : ", route.params);
  getATable(parseInt(route.params.aid));
});
</script>

<style>
.analysis {
  margin: 10px;
}

.analysis-title {
  margin: 10px 0px;
  font-size: 24px;
}
</style>